﻿
Imports System.Data.OleDb
Imports System.Xml
Imports System.ServiceModel.Syndication

Public Class RssFeed
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.ContentType = "application/rss+xml"

        Dim rss As New SyndicationFeed
        rss.Title = TextSyndicationContent.CreatePlaintextContent("most recent picture")
        rss.Description = TextSyndicationContent.CreatePlaintextContent("Most Recent Photo Sharing AppsList in the website")
        rss.Links.Add(SyndicationLink.CreateSelfLink(New Uri(okURL("Default.aspx"))))
        rss.Language = "en-uk"

        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("lensPicConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim cmdQuery As String = "SELECT TOP 2 * from [lenspic]"
        Dim dbcomm = New OleDbCommand(cmdQuery, oleDbConn)
        Dim readDB As OleDbDataReader
        readDB = dbcomm.ExecuteReader()



        Dim items As New List(Of SyndicationItem)
        While readDB.Read()
            Dim sItem As New SyndicationItem
            sItem.Title = mkRssTxt(readDB("PictureTitle"))
            sItem.Summary = mkRssTxt(readDB("Location"))
            items.Add(sItem)
        End While

        rss.Items = items

        Dim writerss As XmlWriter = XmlWriter.Create(Response.OutputStream)

        Dim formatrss = New Rss20FeedFormatter(rss)

        formatrss.WriteTo(writerss)
        writerss.Close()

    End Sub

    Private Function mkRssTxt(ByVal someString As String) As TextSyndicationContent
        Return TextSyndicationContent.CreatePlaintextContent(someString)
    End Function

    Private Function okURL(ByVal urlStr As String)
        Return Request.Url.GetLeftPart(UriPartial.Authority) & ResolveUrl(urlStr)
    End Function

End Class